package com.dengpp.pdform.holder;

import android.graphics.drawable.GradientDrawable;
import android.view.View;
import android.view.ViewGroup;
import android.widget.RadioButton;
import android.widget.TextView;
import androidx.annotation.NonNull;
import com.dengpp.pdform.R;
import com.dengpp.pdform.adapter.AttributeAdapter;
import com.dengpp.pdform.config.AttributeConfig;
import com.google.android.flexbox.FlexboxLayout;
import com.dengpp.pdform.base.BaseAttributeViewHolder;
import java.util.List;

public class RadioViewHolder extends BaseAttributeViewHolder {
    private TextView tvTitle;
    private FlexboxLayout flexboxLayout;
    private TextView tvValue;
    private List<String> options;
    private String currentValue;

    public RadioViewHolder(@NonNull View itemView) {
        super(itemView);
        tvTitle = itemView.findViewById(R.id.tv_title);
        flexboxLayout = itemView.findViewById(R.id.flexbox_layout);
        tvValue = itemView.findViewById(R.id.tv_value);
    }

    @Override
    public void bind(AttributeConfig attribute, boolean isEditMode, AttributeAdapter.OnAttributeChangeListener listener) {
        // 使用带星号的标题
        tvTitle.setText(getTitleWithRequiredStar(attribute.getTitle(), attribute.isRequired()));
        this.options = attribute.getOptions();
        this.currentValue = attribute.getValue() != null ? attribute.getValue().toString() : null;

        if (isEditMode && attribute.isEditable()) {
            flexboxLayout.setVisibility(View.VISIBLE);
            tvValue.setVisibility(View.GONE);

            // 清除之前的选项
            flexboxLayout.removeAllViews();

            if (options != null) {
                for (int i = 0; i < options.size(); i++) {
                    String option = options.get(i);
                    RadioButton radioButton = createRadioButton(option, option.equals(currentValue));
                    radioButton.setTag(option); // 使用tag存储选项值

                    // 设置点击监听器
                    radioButton.setOnClickListener(v -> {
                        String selectedValue = (String) v.getTag();
                        currentValue = selectedValue;

                        // 更新所有按钮的选中状态
                        updateRadioButtonsState();

                        // 通知监听器
                        if (listener != null) {
                            listener.onAttributeChanged(attribute.getKey(), selectedValue);
                        }
                    });

                    flexboxLayout.addView(radioButton);
                }
            }

        } else {
            flexboxLayout.setVisibility(View.GONE);
            tvValue.setVisibility(View.VISIBLE);
            tvValue.setText(currentValue != null ? currentValue : "");
        }
    }
    /**
     * 在 ViewHolder 被回收时清理资源
     */
    @Override
    public void onViewRecycled() {
        super.onViewRecycled();
        // 移除所有单选按钮的点击监听器
        for (int i = 0; i < flexboxLayout.getChildCount(); i++) {
            View child = flexboxLayout.getChildAt(i);
            if (child instanceof RadioButton) {
                child.setOnClickListener(null);
            }
        }
    }
    private RadioButton createRadioButton(String text, boolean isChecked) {
        RadioButton radioButton = new RadioButton(itemView.getContext());

        // 设置布局参数
        FlexboxLayout.LayoutParams layoutParams = new FlexboxLayout.LayoutParams(
                ViewGroup.LayoutParams.WRAP_CONTENT,
                ViewGroup.LayoutParams.WRAP_CONTENT
        );
        layoutParams.setMargins(0, 0, 16, 8);
        radioButton.setLayoutParams(layoutParams);

        // 设置文本
        radioButton.setText(text);
        radioButton.setTextSize(14);
        radioButton.setPadding(24, 12, 24, 12);

        // 移除默认按钮
        radioButton.setButtonDrawable(null);

        // 设置选中状态和样式
        updateRadioButtonAppearance(radioButton, isChecked);

        return radioButton;
    }

    private void updateRadioButtonAppearance(RadioButton radioButton, boolean isChecked) {
        GradientDrawable background = new GradientDrawable();
        background.setShape(GradientDrawable.RECTANGLE);
        background.setCornerRadius(20);

        if (isChecked) {
            background.setColor(itemView.getContext().getResources().getColor(R.color.library_colorPrimary));
            background.setStroke(2, itemView.getContext().getResources().getColor(R.color.library_colorPrimary));
            radioButton.setTextColor(itemView.getContext().getResources().getColor(R.color.library_white));
        } else {
            background.setColor(itemView.getContext().getResources().getColor(R.color.library_white));
            background.setStroke(2, itemView.getContext().getResources().getColor(R.color.library_gray_light));
            radioButton.setTextColor(itemView.getContext().getResources().getColor(R.color.library_gray_dark));
        }

        radioButton.setBackground(background);
        radioButton.setChecked(isChecked);
    }

    private void updateRadioButtonsState() {
        for (int i = 0; i < flexboxLayout.getChildCount(); i++) {
            View child = flexboxLayout.getChildAt(i);
            if (child instanceof RadioButton) {
                RadioButton radioButton = (RadioButton) child;
                String optionValue = (String) radioButton.getTag();
                updateRadioButtonAppearance(radioButton, optionValue.equals(currentValue));
            }
        }
    }
}
